package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysUserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.utils.DateUtils;
import com.itheima.utils.PasswordToEncodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserDao sysUserDao;

    //新增弹框回显用户角色
    @Override
    public List<Role> findRoles() {
       List<Role> roleList = sysUserDao.findRoles();
        return roleList;
    }

    //新增弹框新增功能
    @Override
    @Transactional
    public void add(Integer[] roleIds, SysUser sysUser) {
        //1.密码加密（安全框架）
        //1.1 调用工具类
        Boolean b = PasswordToEncodeUtils.PasswordToEncode(sysUser);
        sysUserDao.add(sysUser); //添加用户（回显主键）
        setRelation(sysUser,roleIds); //添加表关系
    }

    //添加用户和用户角色关系
    private void setRelation(SysUser sysUser, Integer[] roleIds) {
        if (sysUser.getId() != null && roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                sysUserDao.setRelation(sysUser.getId(),roleId);
            }
        }
    }

    //分页功能
    @Override
    public  PageResult findPage(QueryPageBean queryPageBean) {
        //分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //调用dao
        Page<SysUser> sysUserPage = sysUserDao.findPage(queryPageBean.getQueryString());
        List<SysUser> pageList = sysUserPage.getResult();
        for (SysUser sysUser : pageList) {
            sysUser.setPassword("********");
        }

        return new PageResult(sysUserPage.getTotal(),sysUserPage);
    }

    //编辑功能:回显用户
    @Override
    public SysUser edit(Integer id) {
        //调用sysUserDao
        SysUser sysUser = sysUserDao.edit(id);
        sysUser.setPassword("********");
        return sysUser;
    }

    //编辑功能:回显关联角色
    @Override
    public List<Integer> findRoleIdsBySysUserId(Integer id) {
        //调用sysUserDao
        List<Integer> roleIds = sysUserDao.findRoleIdsBySysUserId(id);
        return roleIds;
    }

    //编辑修改
    @Override
    @Transactional
    public void update(Integer[] roleIds, SysUser sysUser) {
        //获取用户表id
        Integer sysUserId = sysUser.getId();
        //删除表关系
        deleteRelation(sysUserId);
        //修改用户表
        sysUserDao.update(sysUser);
        //关联表关系
        setRelation(sysUser,roleIds);
    }

    //删除用户和角色的表关系
    private void deleteRelation(Integer sysUserId) {
        //2.删除该用户信息
        sysUserDao.deleteRelation(sysUserId);
    }

    //删除功能
    @Override
    @Transactional
    public void delete(Integer id) {
        //1.删除表关系
        deleteRelation(id);
        //2.删除用户
        sysUserDao.deleteRoleById(id);
    }

    //校验姓名
    @Override
    public Boolean checkUsername(String username) {
        //调用dao
        SysUser sysUser = sysUserDao.checkUsername(username);
        if (sysUser != null) {
            return false;
        }
        return true;
    }

    //校验电话
    @Override
    public Boolean checkTelephone(String telephone) {
        //调用dao
        Long count = sysUserDao.checkTelephone(telephone);
        if (count>0) {
            return false;
        }
        return true;
    }

    //登录名查询用户id
    @Override
    public Integer findByUsername(String username) {
        return sysUserDao.findByUsername(username);
    }
}
